<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="org.apache.catalina.Session"%>
<%@ page import="java.sql.Date,java.sql.*"%>
<%@ page import="cn.ticket.db.DBConnect"%>
<%@ page import="cn.ticket.util.StringUtil"%>
<%@ page import="cn.ticket.Constant"%>
    <%
    request.setCharacterEncoding("utf8" ) ;
	String num = request.getParameter("num") ;
	String oriStation1 = request.getParameter("oriStation1" ) ;
	String oriStation2 = request.getParameter("oriStation2" ) ;
	String oriStation = oriStation1+"-"+oriStation2 ;
	String desStation1 = request.getParameter("desStation1" ) ;
	String desStation2 = request.getParameter("desStation2" ) ;
	String desStation = desStation1+"-"+desStation2 ;
	String price_ =request.getParameter("price") ;
	int price=(price_==null || "".equals(price_))?0:Integer.parseInt(price_) ;
	String time = request.getParameter("time") ;
	int total = null==request.getParameter("total")?0:Integer.parseInt(request.getParameter("total") ) ;
	String userId= (String)session.getAttribute("login_user_id") ;
	if(null==num || "".equals(num)){
		session.setAttribute("errorinfo", "车次编号"+Constant.ERRORINFO_PARAMETERNULL) ;
		%>
		<jsp:forward page="../front/result.jsp" />
		<%
	}
	if(null==oriStation || "".equals(oriStation)){
		session.setAttribute("errorinfo", "出发站"+Constant.ERRORINFO_PARAMETERNULL) ;		
		%>
		<jsp:forward page="../result.jsp" />
		<%
	}
	if(null==desStation || "".equals(desStation) ){
		session.setAttribute("errorinfo", "终点站"+Constant.ERRORINFO_PARAMETERNULL) ;
		%>
		<jsp:forward page="../result.jsp" />
	<%
	}	if(null==time || "".equals(time)){
		session.setAttribute("errorinfo", "开车时间"+Constant.ERRORINFO_PARAMETERNULL) ;
		%>
		<jsp:forward page="../result.jsp" />
		<%
	}	if(null==userId || "".equals(userId)){
		session.setAttribute("errorinfo", "用户"+Constant.ERRORINFO_PARAMETERNULL) ;		
	%>
	<jsp:forward page="../result.jsp" />
	<%
}if(total<=0){
	session.setAttribute("errorinfo", "座位数"+Constant.ERRORINFO_PARAMETERERROR) ;		
%>
<jsp:forward page="../result.jsp" />
<%
}
	String id = StringUtil.createUUID() ;
	Connection con = new DBConnect().DBConnection() ;
	PreparedStatement pstmt = null ;
	try {
		pstmt =  con.prepareStatement("insert into bus(id,num,oriStation,desStation,price,time,creTime,userId,total,remain) values(?,?,?,?,?,?,?,?,?,?)") ;
		pstmt.setString(1,id) ;
		pstmt.setString(2,num) ;
		pstmt.setString(3,oriStation) ;
		pstmt.setString(4,desStation) ;
		pstmt.setInt(5,price) ;
		pstmt.setString(6, time) ;
		pstmt.setDate(7, new Date(new java.util.Date().getTime())) ;
		pstmt.setString(8, userId ) ; 
		pstmt.setInt(9, total) ;
		pstmt.setInt(10, total) ;
		pstmt.execute();
	 %>
		<jsp:forward page="busList.jsp" />
		<%
	}catch(Exception e){
		session.setAttribute("errorinfo", Constant.ERRORINFO_NETERROR+e) ;		
		%>
		<jsp:forward page="../result.jsp?result=系统异常了,请稍后再操作." />
		<%
	}finally{
		pstmt.close();
		con.close() ;
	}
%>
